using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Threading;
using System.IO;

namespace Framework.Module.Adsvertising.Lib
{
    public class UploadFile
    {
        private string _strFileName;

        public string GetFileName
        {
            get
            {
                return this._strFileName;
            }
            set
            {
                this._strFileName = value;
            }
        }

        public bool UploadImage(FileUpload objFile, string strPath)
        {
            if (this.CheckPath(strPath))
            {
                if (objFile.HasFile)
                {
                    this._strFileName = objFile.FileName.Substring(objFile.FileName.LastIndexOf("."));
                    this._strFileName = this.RenameFile(DateTime.Now) + this._strFileName;
                    FileInfo fileinfo = new FileInfo(this._strFileName);
                    
                        try
                        {
                            objFile.SaveAs(strPath + this._strFileName);
                            return true;
                        }
                        catch (Exception)
                        {
                            return false;
                        }
                    
                }
                return false;
            }
            return false;
        }
        public bool CheckPath(string strPath)
        {
            if (!Directory.Exists(strPath))
            {
                try
                {
                    Directory.CreateDirectory(strPath);
                    return true;
                }
                catch
                {
                    return false;
                }
            }
            return true;
        }
        private string RenameFile(DateTime objDate)
        {
            return ((((objDate.Day.ToString() + objDate.Month.ToString() + objDate.Year.ToString()) + "_" + objDate.Hour.ToString()) + objDate.Minute.ToString() + objDate.Second.ToString()) + objDate.Millisecond.ToString() + "_Framework");
        }
      
    }
}
